package com.probox.common.entity.service.order.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.order.OrderPayBackMapper;
import com.probox.common.entity.domain.order.OrderPayBack;
import com.probox.common.entity.service.order.IOrderPayBackService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户退款信息Service业务层处理
 *
 * @author hds
 * @date 2021-06-07
 */
@Service
@Transactional
public class OrderPayBackServiceImpl extends ServiceImpl<OrderPayBackMapper, OrderPayBack> implements IOrderPayBackService {
    @Autowired
    private OrderPayBackMapper orderPayBackMapper;


    /**
     * 查询用户退款信息
     *
     * @param backUid 用户退款信息ID
     * @return 用户退款信息
     */
    @Override
    public OrderPayBack selectOrderPayBackById(Long backUid) {
        return this.getById(backUid);
    }

    /**
     * 查询用户退款信息列表
     *
     * @param orderPayBack 用户退款信息
     * @return 用户退款信息
     */
    @Override
    public List<OrderPayBack> queryList(OrderPayBack orderPayBack) {
        QueryWrapper<OrderPayBack> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getBackUid())) && orderPayBack.getBackUid() != null) {
            queryWrapper.eq("BACK_UID", orderPayBack.getBackUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getSupplierUid())) && orderPayBack.getSupplierUid() != null) {
            queryWrapper.eq("SUPPLIER_UID", orderPayBack.getSupplierUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getSupplierName())) && orderPayBack.getSupplierName() != null) {
            queryWrapper.eq("SUPPLIER_NAME", orderPayBack.getSupplierName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getCustomerUid())) && orderPayBack.getCustomerUid() != null) {
            queryWrapper.eq("CUSTOMER_UID", orderPayBack.getCustomerUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getCustomerName())) && orderPayBack.getCustomerName() != null) {
            queryWrapper.eq("CUSTOMER_NAME", orderPayBack.getCustomerName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getOrderUid())) && orderPayBack.getOrderUid() != null) {
            queryWrapper.eq("ORDER_UID", orderPayBack.getOrderUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getOrderNo())) && orderPayBack.getOrderNo() != null) {
            queryWrapper.eq("ORDER_NO", orderPayBack.getOrderNo());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getItemUid())) && orderPayBack.getItemUid() != null) {
            queryWrapper.eq("ITEM_UID", orderPayBack.getItemUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getPayAmount())) && orderPayBack.getPayAmount() != null) {
            queryWrapper.eq("PAY_AMOUNT", orderPayBack.getPayAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getBackAmount())) && orderPayBack.getBackAmount() != null) {
            queryWrapper.eq("BACK_AMOUNT", orderPayBack.getBackAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getApplyReason())) && orderPayBack.getApplyReason() != null) {
            queryWrapper.eq("APPLY_REASON", orderPayBack.getApplyReason());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getApplyTime())) && orderPayBack.getApplyTime() != null) {
            queryWrapper.eq("APPLY_TIME", orderPayBack.getApplyTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getStatus())) && orderPayBack.getStatus() != null) {
            queryWrapper.eq("STATUS", orderPayBack.getStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getBackReason())) && orderPayBack.getBackReason() != null) {
            queryWrapper.eq("BACK_REASON", orderPayBack.getBackReason());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getConfirmTime())) && orderPayBack.getConfirmTime() != null) {
            queryWrapper.eq("CONFIRM_TIME", orderPayBack.getConfirmTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getAuditRemark())) && orderPayBack.getAuditRemark() != null) {
            queryWrapper.eq("AUDIT_REMARK", orderPayBack.getAuditRemark());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getBackStatus())) && orderPayBack.getBackStatus() != null) {
            queryWrapper.eq("BACK_STATUS", orderPayBack.getBackStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getRefundId())) && orderPayBack.getRefundId() != null) {
            queryWrapper.eq("REFUND_ID", orderPayBack.getRefundId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayBack.getBackType())) && orderPayBack.getBackType() != null) {
            queryWrapper.eq("BACK_TYPE", orderPayBack.getBackType());
        }
        return orderPayBackMapper.selectList(queryWrapper);
    }

    /**
     * 新增用户退款信息
     *
     * @param orderPayBack 用户退款信息
     * @return 结果
     */
    @Override
    public boolean insertOrderPayBack(OrderPayBack orderPayBack) {

        return this.save(orderPayBack);

    }

    /**
     * 修改用户退款信息
     *
     * @param orderPayBack 用户退款信息
     * @return 结果
     */
    @Override
    public boolean updateOrderPayBack(OrderPayBack orderPayBack) {

        return this.updateById(orderPayBack);
    }

    /**
     * 批量删除用户退款信息
     *
     * @param backUids 需要删除的用户退款信息ID
     * @return 结果
     */
    @Override
    public boolean deleteOrderPayBackByIds(Long[] backUids) {
        return this.removeByIds(Arrays.asList(backUids));
    }

    /**
     * 订单已退款金额
     * @param orderNo
     * @return
     */
    @Override
    public Long sumBackByOrderNo(String orderNo) {
        if(ObjectUtil.isEmpty(orderNo))
            throw new BaseException("订单编号不能为空");
        return orderPayBackMapper.sumBackByOrderNo(orderNo);
    }


}
